package com.erp.module.yuliaofreight.inYuliaoGiFreight

import android.content.Context
import com.erp.Interface.OnUserDataInterface
import com.erp.module.yuliaofreight.YuliaoFreightConteract
import com.erp.module.yuliaofreight.YuliaoFreightModel
import com.erp.util.JsonUtil
import org.json.JSONException
import org.json.JSONObject

class IinYuliaoGiFreightP(var V:YuliaoFreightConteract.inYuliaoGiFreightView,var  context : Context): YuliaoFreightConteract.inYuliaoGiFreight {
    var md = YuliaoFreightModel()
    override fun inYuliaoRucang(yfs_id: String, gi_id: String,Lenght:String,Width:String,Thickness:String,rucangcode:String) {
        md.inYuliao(context,yfs_id,gi_id,Lenght.trim(),Width.trim(),Thickness.trim(),rucangcode,object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                V.inYuliaoChucangOk(data.toString())
            }

            override fun onNot(throwable: String?) {
                V.inYuliaoChucangNo(throwable.toString())
            }

        })
    }
    override fun getCangweiData() {
        md.getCangweiData(context,object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                JsonCangwei(data)
            }

            override fun onNot(throwable: String?) {

            }

        })
    }
    override fun getBianmaData(leibie:String) {
        md.getBianmaData(context,leibie,object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                JsonBianma(data)
            }

            override fun onNot(throwable: String?) {
            }

        })
    }

    fun JsonCangwei(data: String?){
        if (data==null){
            return
        }
        var cangweinames: MutableList<String> = ArrayList<String>()
        var cangweiids: MutableList<String> = ArrayList<String>()
        try {
            val j = JSONObject(data)
            val js = JsonUtil.getjsvolue(j,"data")
            for (i in 0 until js.length()) {
                val jo = js.get(i) as JSONObject
                cangweiids.add(JsonUtil.getvolue(jo, "ysf_id"))
                cangweinames.add(JsonUtil.getvolue(jo, "freight_space_name"))
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        V.getCangwei(cangweiids,cangweinames)
    }

    fun JsonBianma(data: String?){
        if (data==null){
            return
        }
        var bianmanames: MutableList<String> = ArrayList<String>()
        var bianmaids: MutableList<String> = ArrayList<String>()
        try {
            val j = JSONObject(data)
            val js = JsonUtil.getjsvolue(j,"data")
            for (i in 0 until js.length()) {
                val jo = js.get(i) as JSONObject
                bianmaids.add(JsonUtil.getvolue(jo, "id"))
                bianmanames.add(JsonUtil.getvolue(jo, "code"))
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        V.getBianma(bianmaids,bianmanames)
    }
}